package com.mav.service.iml;

import com.mav.anno.LogOperation;
import com.mav.mapper.DeptMapper;
import com.mav.pojo.Dept;
import com.mav.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class DeptServiceIml implements DeptService {
    @Autowired
    DeptMapper deptMapper;
    @LogOperation
    @Override
    public List<Dept> findAll() {

        return deptMapper.findAll();
    }
    @LogOperation
    @Override
    public void deleteById(Integer id) throws DeleteException {
        Long total=deptMapper.findEmp(id);
        if(total==0){
            deptMapper.deleteById(id);
        }else throw new DeleteException("对不起，当前部门下有员工，不能直接删除！");

    }

    @Override
    public void add(Dept dept) {
        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.insert(dept);
    }

    @Override
    public Dept getInfo(Integer id) {
        return deptMapper.getInfo(id);
    }

    @Override
    public void update(Dept dept) {
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.update(dept);
    }
}
